// pages/orders/evaluate.js
import base from '../../config/base.js'
import Router from '../../utils/router.js'
import Service from '../../services/orderinfo.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [
      {
       url: `${base.static}/orders/start-yellow.png`,
       types: 1
      },
      {
        url: `${base.static}/orders/start-yellow.png`,
        types: 1
      },
      {
        url: `${base.static}/orders/start-yellow.png`,
        types: 1
      },
      {
        url: `${base.static}/orders/start-yellow.png`,
        types: 1
      },
      {
        url: `${base.static}/orders/start-yellow.png`,
        types: 1
      }],
    orid: '',
    goodslist: '',
    startnum:  5
  },
  // 获取商品信息
  getgoodsdetail(){
    let that = this;
    let param = {
      orid: this.data.orid
    }
    Service.queryorderdetails(param, function (res) {
      if (res && res.code == 1) {
        let list = res.data,
          str_attr_val_name_obj = list.str_attr_val_name,
          str_attr_val_name = '';
        for (let k in str_attr_val_name_obj) {
          str_attr_val_name += k + ': ' + str_attr_val_name_obj[k] + ' ';
        }
        list.str_attr_val_name = str_attr_val_name;
        that.setData({
          goodslist: list
        })
      }
    })
  },
  // 点击星星
  showStatHandle(e){
    let that = this
    let index = e.currentTarget.dataset.index;
    let types = e.currentTarget.dataset.types;
    that.data.startnum = index;
    if (types == 1){//减星
      for (let i = 5; i > index; i--) {
        that.setData({
          [`imgList[${i - 1}]`]:{
            url: base.static + '/orders/start-gray.png',
            types: 2
          } 
        })
      }
    }else{//加星
      for (let j = 0; j < index; j++) {
        that.setData({
          [`imgList[${j}]`]: {
            url: base.static + '/orders/start-yellow.png',
            types: 1
          } 
        })
      }
    }

    
  },
  // 提交分数
  goSubmitHandle(){
    let that = this;
    let param = {
      orid: that.data.orid,
      score: that.data.startnum
    }
    Service.savegoodsappraises(param,function(res){
      if(res && res.code == 1){
        if(that.data.page == 'list'){
          Router.go('orderList', '', 'redirectTo')          
        }else {
          Router.go('orderDetail', '', 'redirectTo')          
        }
    
        setTimeout(()=>{
          wx.showToast({
            title: '评价成功',
          })
        },500)
      }
    })
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let orid = options.orid;
    that.setData({
      orid: orid,
      page: options.page
    })
    that.getgoodsdetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})